package org.kineticsproject.visualizations;

import java.awt.Color;
import java.util.Date;
import java.util.Iterator;

import org.kineticsproject.devices.parsers.UnitConverter;

import data.DataLog;
import data.DataSample;
import data.FusedDataSample;


public class GeoVisualizer {
	private Trace 		trace;
	private DataLog 	act;
	
	
	public GeoVisualizer(DataLog act, Trace trace) 
	{
		this.trace  = trace;
		this.act 	= act;
	}

	private int max(int input,int max)
	{
		int result = input;
		if (input < 0)
			result = 0;
		else
		if (input > max)
			result = max;

		return(result);
	}
	
	public void process()
	{
		int iTemp;
		int iPreviousCrumbWidth = 4;
		int iCrumbWidth = 0;
		FusedDataSample sample;
		//Color  crumbColor;
		//sample = (FusedDataSample)act.getDataSamples().get(0);

		
		Iterator<DataSample> it = act.getDataSamples().iterator();
		
		while (it.hasNext())
		{
			sample = (FusedDataSample)it.next();
			Color crumbColor = Color.BLUE;
			if (sample.getHeartratesample() != null)
			{
		/*		if ((sample.getHeartratesample().getHeartRate() < 132) )
					crumbColor = Color.WHITE;
				if ((sample.getHeartratesample().getHeartRate() < 149) && (sample.getHeartratesample().getHeartRate() >= 132))
						crumbColor = Color.YELLOW;
				else if ((sample.getHeartratesample().getHeartRate() >= 149) && (sample.getHeartratesample().getHeartRate() < 166))
					crumbColor = Color.ORANGE;
				else if (sample.getHeartratesample().getHeartRate() >= 166)
					crumbColor = Color.RED;*/
		
				iTemp = 4*(sample.getHeartratesample().getHeartRate() - 80);
				
				iCrumbWidth = (int)UnitConverter.KilometerstoMiles(sample.getDisplacementsample().getSpeed())*2;
				iCrumbWidth = (int)UnitConverter.lowpassfilter(0.6, iPreviousCrumbWidth, iCrumbWidth);			
				
				
			//	crumbColor = new Color(max(2*(sample.getHeartratesample().getHeartRate()-80),254),10,max(250-(2*(sample.getHeartratesample().getHeartRate()-80)),254));
			//	crumbColor = new Color(max(4*(sample.getHeartratesample().getHeartRate()-100),254),max(4*(sample.getHeartratesample().getHeartRate()-160),254),0);
				crumbColor = new Color(max(iTemp,254),max(500-iTemp,254),0);
			}
			trace.add(new Crumb(String.valueOf(sample.getPositionsample().getLatitude()),
					String.valueOf(sample.getPositionsample().getLongitude()),
					String.valueOf(sample.getPositionsample().getAltitude()),
					crumbColor,sample.getDisplacementsample().getSpeed(),0,iCrumbWidth,
					new Date(sample.getTimestamp().getTime())));
			
		//sample = (FusedDataSample)act.getDataSamples().;
		}
	}
}
